package com.seeyon.ctp.portal.section;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.seeyon.ctp.portal.section.BaseSectionImpl;
import com.seeyon.ctp.portal.section.templete.BaseSectionTemplete;
import com.seeyon.ctp.portal.section.templete.HtmlTemplete;

public class SinaNewsSection extends BaseSectionImpl {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}
	
	@Override
    public String getId() {
        //栏目ID，必须与spring配置文件中的ID相同;如果是原栏目改造，请尽量保持与原栏目ID一致
        return "sinaNewsSection";
    }

    @Override
    public String getName(Map<String, String> preference) {
        //栏目显示的名字，必须实现国际化，在栏目属性的“columnsName”中存储
        String name = preference.get("columnsName");
        if(StringUtils.isBlank(name)){
            return "新浪新闻";
        }else{
            return name;
        }
    }

    @Override
    public Integer getTotal(Map<String, String> preference) {
        //栏目需要展现总数据条数时重写
        return null;
    }

    @Override
    public String getIcon() {
        // 栏目图标，暂不需要实现
        return null;
    }

    @Override
    public BaseSectionTemplete projection(Map<String, String> preference) {
        
        HtmlTemplete ht = new HtmlTemplete();
        StringBuilder html = new StringBuilder();
        html.append(crawlArticleList());
        ht.setHeight("230");
        ht.setHtml(html.toString());
        ht.setModel(HtmlTemplete.ModelType.inner);
        ht.setShowBottomButton(true);
        ht.addBottomButton(BaseSectionTemplete.BOTTOM_BUTTON_LABEL_MORE, "javascript:window.open('http://roll.news.sina.com.cn');");
        return ht;
    }
	
	public String crawlArticleList() {
		Document doc;
        StringBuffer sb = new StringBuffer("");
        int count = 0;
        try {
            doc = Jsoup.connect("http://roll.news.sina.com.cn").timeout(5000).get();
            Elements ListLi = doc.getElementsByTag("li");
            for (Element element :ListLi) {
            	if(count > 9) {
            		break;
            	}
            	Elements links = element.getElementsByTag("a");
                Elements times = element.getElementsByAttributeValue("class","c_time");
                String strtime = "";
                for (Element time : times) {
                	strtime = time.text().trim();
                }
                for (Element link : links) {
                    String linkHref = link.attr("href");
                    String linkText = StringFilter(link.text().trim());
                    sb.append("<div style=\"font-size: 14px;padding-right:10px\">");
                    sb.append((new StringBuilder("<a href=\"")).append(linkHref).append("\" target=\"_blank\">").append(linkText).append("</a>").toString());
                    sb.append("<span style=\"color:#8e8e8e;display:inline-block;float:right\">"  + strtime + "</span></div>");
                }
                count++;
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
	
	public String StringFilter(String str) throws PatternSyntaxException {     
	    String regEx="�";  
	    Pattern p = Pattern.compile(regEx);     
	    Matcher m = p.matcher(str);     
	    return m.replaceAll("").trim();     
	}     

}
